package com.myspring.web.view.excel;

import com.myspring.web.form.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * response注解解析RequestResponseBodyMethodProcessor
 * @author Administrator
 *
 *1、直接写入视图引用
 *2、自定义解析器，并配置到配置文件，针对xls结尾的视图用excel解析器
 *3、自定义注解，参考RequestResponseBodyMethodProcessor，将返回值直接处理
 *
 *我们有jsp视图解析器不配置优先级，自定义的视图解析器不起作用 TODO
 */
@Controller
@RequestMapping("/excel")
public class ExcelViewController {
	
	@RequestMapping("/init")
	public ModelAndView init() {
		return new ModelAndView("excel/excel");
	}

	/**
	 * 通过视图引用下载excel
	 * @return
	 */
	@RequestMapping("/download")
	public ModelAndView test() {
		ModelAndView mav = new ModelAndView();
		ExcelView excelView = new ExcelView();
		List<Student> list = new ArrayList<>();
		
		Student student = new Student();
		student.setId(1);
		student.setName("hello");
		list.add(student);
		
		student = new Student();
		student.setId(2);
		student.setName("world");
		list.add(student);
		
		//如果list有上千万或者亿级别的结果急，可以拆分list成多个list，写入多个sheet
		//分页多个查询放到Map里面
		mav.addObject("list", list);
		
		mav.setView(excelView);
		return mav;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/download1")
	public ModelAndView excel1() {
		//针对指定的视图解析
		ModelAndView mv = new ModelAndView("excelView");
		List<Student> list = new ArrayList<>();
		
		Student student = new Student();
		student.setId(1);
		student.setName("你好");
		list.add(student);
		
		student = new Student();
		student.setId(2);
		student.setName("世界");
		list.add(student);
		
		mv.addObject("list", list);
		return mv;
	}
	
	@RequestMapping("/download2")
	@Excel
	public List<Student> excel2() {
		return this.buildStudentList();
	}

	/**
	 * 参考HandlerMethodReturnValueHandlerComposite，Map返回类型会有个处理器MapMethodProcessor
	 * 由于RequestResponseBodyMethodProcessor在Map处理器之前，所以可以解析Map返回类型
	 */
	@RequestMapping("/download/excelEntry")
	public ExcelEntry<Student> excelEntry() {
		ExcelEntry<Student> studentExcelEntry = new ExcelEntry<>();
		studentExcelEntry.setTitleList(Arrays.asList("id", "名字"));
		Map<String, List<Student>> map = new LinkedHashMap<>();
		map.put("1", this.buildStudentList());
//		map.put("2", this.buildStudentList());
		studentExcelEntry.setContentMap(map);

		return studentExcelEntry;
	}

	@RequestMapping("/download4")
	@ResponseBody
	public ModelMap excel4(ModelMap modelMap) {
		modelMap.addAttribute(ExeclConstants.EXCEL_HEADER, Arrays.asList("id", "名字"));
		modelMap.addAttribute(this.buildStudentList());
		modelMap.addAttribute(this.buildStudentList());
		return modelMap;
	}

	/**
	 * 方法的参数里面并没有Map类型，为什么还不能Excel??
	 * @return
	 */
	@RequestMapping("/download5")
	@Excel
	public Map<String, List<Student>> excel5() {
		Map<String, List<Student>> map = new HashMap<>();
		map.put("1", this.buildStudentList());
		return map;
	}

	/**
	 *
	 * @return
	 */
	@RequestMapping("/download6")
	@ResponseBody
	public Map<String, List<Student>> excel6() {
		Map<String, List<Student>> map = new HashMap<>();
		map.put("1", this.buildStudentList());
		return map;
	}

	@RequestMapping("/download7")
	public List<Student> excel7() {
		return this.buildStudentList();
	}

	public List<Student> buildStudentList() {
		List<Student> list = new ArrayList<>();

		Student student = new Student();
		student.setId(1);
		student.setName("Tom");
		list.add(student);

		student = new Student();
		student.setId(2);
		student.setName("Jerry");
		list.add(student);

		return list;
	}
}
